/**
 *
 * @properties={typeid:24,uuid:"2009020E-8AD0-464F-9B59-0239105845FC"}
 */
function camposVisibles() {
	elements.grp_impresion.visible 	= impresion
	elements.grp_csv.visible 		= exporta_csv
	elements.grp_alta.visible		= alta
	elements.grp_baja.visible		= baja
	elements.grp_modif.visible		= modificacion
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"6B3AB09D-AAA8-4D9D-9C7C-982AD02A2B72"}
 */
function cargaraUsuarios(event) {
	var $rel = pantallas_to_rel_pantallas_usuarios
	if ($rel.getSize() == 0)
	{
		var $cant = pantallas_to_rel_solucion_usuario.getSize()
		for (var i = 1 ; i <= $cant ; i++)
		{
			var $reg = pantallas_to_rel_solucion_usuario.getRecord(i)
			$rel.newRecord()
			$rel.visible 			= 1
			$rel.modificable		= 1
			$rel.nrogrupotree		= nrogrupotree
			$rel.orden_aparicion	= orden_aparicion
			$rel.solucion_id		= id_solucion
			$rel.usuario_id			= $reg.usuario_id
		}
		globals.DIALOGS.showInfoDialog('Aviso','Proceso terminado con exito','OK')
	}
	else
	{
		globals.DIALOGS.showWarningDialog('Aviso','Esta pantalla ya está autorizada para los usuarios','OK')
	}
}

/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"84D19205-7955-4997-BE65-F99211252D29"}
 */
function onDataChangeGrupo(oldValue, newValue, event) {
	var $cant = databaseManager.getFoundSetCount(pantallas_to_rel_pantallas_usuarios)
	for (var i = 1 ; i <= $cant ; i++)
	{
		var $reg = pantallas_to_rel_pantallas_usuarios.getRecord(i)
		$reg.nrogrupotree = newValue
	}
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"5510119B-EFA1-4BB3-9325-6074B649DDA5"}
 */
function buscarIconoTree(event) {
	plugins.file.showFileOpenDialog(null, null, false, new Array("Images", "jpg", "gif", "png", "*"), grabarTree, 'Seleccione el logo')
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"425240B3-5AFD-4F07-B6B0-7393AA2011D0"}
 */
function buscarIconoBoton(event) {
	plugins.file.showFileOpenDialog(null, null, false, new Array("Images", "jpg", "gif", "png", "*"), grabarBoton, 'Seleccione el logo')
}

/**
 * @param {Array<plugins.file.JSFile>} foto
 * 
 * @properties={typeid:24,uuid:"C69E6BB6-65D8-41D0-B334-ECCA95F553EA"}
 */
function grabarTree(foto){
	grabarIcono('tree', foto[0], 20, 20)
}

/**
 * @param {Array<plugins.file.JSFile>} foto
 * 
 * @properties={typeid:24,uuid:"D74A5138-6B9D-4A5E-85F8-F4881B997A25"}
 */
function grabarBoton(foto){
	grabarIcono('boton', foto[0], 32, 32)
}

/**
 * @param {String} $tipo
 * @param {plugins.file.JSFile} foto
 * @param {Number} altura
 * @param {Number} ancho
 *
 * @properties={typeid:24,uuid:"5F7B4DC9-3A06-4F61-8201-7C02E378D563"}
 */
function grabarIcono($tipo, foto, altura, ancho){
	var $nombre 	= foto.getName()
	var $extension 	= $nombre.substring($nombre.length - 3,$nombre.length)
	
	if (foto.getContentType().indexOf('image') == -1)
	{
		globals.DIALOGS.showWarningDialog('Aviso','El archivo debe ser una imagen','OK')
		return
	}
	
	var $img = plugins.images.getImage(foto.getBytes())
	if ($img.getHeight() > altura || $img.getWidth() > ancho)
		$img = $img.resize(ancho,altura)
	
	var $data 	= $img.getData()//$arch.getBytes();
	var $exito
	if (application.getApplicationType() == APPLICATION_TYPES.WEB_CLIENT)
	{
		var $dir 	= plugins.file.getDefaultUploadLocation()+ '/' + id_solucion + '/' + $tipo
		var $folder = plugins.file.createFile($dir)
		if (!plugins.file.readFile($folder))
			plugins.file.createFolder($dir)
	
		var $file = plugins.file.createFile($dir+'/' + nombre + '.' + $extension)
		
		if (plugins.file.writeFile($file, $data, foto.getContentType()))
		{
			$exito = 1
		}
		else
			$exito = 0
	}
	else
	{
		try {
			var $dir = '/' + id_solucion + '/' + $tipo
			var tFile = plugins.file.createTempFile("icono", '.' + $extension)
			plugins.file.writeFile(tFile, $data)
			plugins.file.streamFilesToServer(tFile, $dir+'/' + nombre + '.' + $extension);
			$exito = 1
			globals.DIALOGS.showInfoDialog('Aviso','La imagen fue subida exitosamente','OK')
		}
		catch(e)
		{
			$exito = 0
		}
	}
	
	if ($exito)
	{
		if ($tipo == 'tree')
		{
			icono_url = application.getServerURL()+'/uploads/'+ id_solucion + '/' + $tipo + '/' + nombre + '.' + $extension
			elements.lbl_icono_tree.imageURL = icono_url
		}
		else
		{
			url_boton = application.getServerURL()+'/uploads/'+ id_solucion + '/' + $tipo + '/' + nombre + '.' + $extension
			elements.lbl_icono_boton.imageURL = url_boton
		}
	}
	else
		globals.DIALOGS.showWarningDialog('Aviso','No se pudo subir la imagen','OK')
}
/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"CF7632D0-B957-47AB-8C65-F1B69FFDDD12"}
 */
function onShow(firstShow, event) {
	elements.lbl_icono_boton.imageURL = url_boton
	elements.lbl_icono_tree.imageURL = icono_url
	camposVisibles()
}
